package com.sskj.givecoin.v.activity;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sskj.givecoin.MyAppLication;
import com.sskj.givecoin.R;
import com.sskj.givecoin.common.App;
import com.sskj.givecoin.common.Constants;
import com.sskj.givecoin.common.rxbus2.RxBus;
import com.sskj.givecoin.common.rxbus2.RxBusCode;
import com.sskj.givecoin.common.rxbus2.Subscribe;
import com.sskj.givecoin.common.rxbus2.ThreadMode;
import com.sskj.givecoin.p.activity.LoginPresenter;
import com.sskj.givecoin.utils.CheckUtil;
import com.sskj.givecoin.utils.ClickUtil;
import com.sskj.givecoin.utils.SPUtils;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.base.BaseActivity;
import com.sskj.givecoin.v.fragment.LoginFragment;
import com.sskj.givecoin.v.fragment.RegisterFragment;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * <pre>
 *     author : wk_math
 *     e-mail : wk_math@163.com
 *     time   : 2018/03/30
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class LoginActivity extends BaseActivity<LoginPresenter> {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.account_login)
    TextView accountLogin;  // 账号登录
    @BindView(R.id.yzm_login2)
    TextView yzmLogin2;  // 账号登录
    @BindView(R.id.pwd_view)
    View pwdView;  // 账号登录下的下滑线
    @BindView(R.id.yzm_view)
    View yzmView;  // 验证码登录下的下滑线
    @BindView(R.id.frameLayout)
    FrameLayout frameLayout;  // 验证码登录下的下滑线

    private static int index;
    private int showInt = 0;  // 0 账号登录   1  验证码登录
    private DisposableSubscriber<Long> disposableSubscriber;
    private FragmentTransaction ft;
    private LoginFragment loginFragment;
    private RegisterFragment registerFragment;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    public LoginPresenter getPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void onViewBinding() {
        RxBus.getDefault().register(this);
        loginFragment = LoginFragment.newInstance();
        registerFragment = RegisterFragment.newInstance();
        ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.frameLayout, registerFragment);
        ft.add(R.id.frameLayout, loginFragment);
        ft.show(loginFragment);
        ft.commit();
        accountLogin.setScaleX(1.3f);
        accountLogin.setScaleY(1.3f);
        yzmLogin2.setScaleY(1);
        yzmLogin2.setScaleX(1);

        ClickUtil.click(400, accountLogin, () -> {
            changeLogin();
        });
        ClickUtil.click(400, yzmLogin2, () -> {
            if (showInt == 1)
                return;
            showInt = 1;
            accountLogin.setTextColor(getResources().getColor(R.color.app_323));
            yzmLogin2.setTextColor(getResources().getColor(R.color.app_664));
            accountLogin.setScaleX(1f);
            accountLogin.setScaleY(1f);
            yzmLogin2.setScaleY(1.3f);
            yzmLogin2.setScaleX(1.3f);
            pwdView.setVisibility(View.GONE);
            yzmView.setVisibility(View.VISIBLE);
            ft = getSupportFragmentManager().beginTransaction();
            ft.hide(loginFragment).show(registerFragment).commit();
        });
        ClickUtil.click(ivBack, () -> finish());

    }

    private void changeLogin() {
        if (showInt == 0) {
            return;
        }
        showInt = 0;
        accountLogin.setTextColor(getResources().getColor(R.color.app_664));
        yzmLogin2.setTextColor(getResources().getColor(R.color.app_323));


        accountLogin.setScaleX(1.3f);
        accountLogin.setScaleY(1.3f);
        yzmLogin2.setScaleY(1);
        yzmLogin2.setScaleX(1);
        pwdView.setVisibility(View.VISIBLE);
        yzmView.setVisibility(View.GONE);
        ft = getSupportFragmentManager().beginTransaction();
        ft.hide(registerFragment).show(loginFragment).commit();
    }

    @Subscribe(code = RxBusCode.REGISTER_SUCCESS, threadMode = ThreadMode.MAIN)
    public void registerSuccess() {
        changeLogin();
    }

    @Override
    protected void initView() {
        index = getIntent().getIntExtra("index", 0);
    }

    /**
     * 登录
     *
     * @param context context
     */
    public static void start(Context context, int index) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra("index", index);
        context.startActivity(intent);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }


    /**
     * 登录成功
     */
    public void loginSuccess(int type) {  // 0 关闭  1 开启
//        RxBus.getDefault().send(Constants.USER_LOGIN);
        if (index != 0) {
            if (0 == type) {
                SPUtils.put(MyAppLication.getApplication(), Constants.SP_LOGIN_STATUS, true);
                finish();
            } else {
                GoogleLoginActivity.start(LoginActivity.this, 0, null);
            }
        } else {
            if (0 == type) {
                SPUtils.put(MyAppLication.getApplication(), Constants.SP_LOGIN_STATUS, true);
                startActivity(new Intent(this, MainActivity.class));
                finish();
            } else {
                GoogleLoginActivity.start(LoginActivity.this, 0, null);
                finish();
            }

        }
    }

//    public void startCount() {
//        registerGetAuthCode.setEnabled(false);
//        disposableSubscriber = new DisposableSubscriber<Long>() {
//            @Override
//            public void onNext(Long aLong) {
//                int time = (int) (60 - aLong);
//                registerGetAuthCode.setText(String.format(App.INSTANCE.getString(R.string.strRegisterActivity210), time));
//            }
//
//            @Override
//            public void onError(Throwable t) {
//                System.out.println(t.toString());
//            }
//
//            @Override
//            public void onComplete() {
//                registerGetAuthCode.setText(App.INSTANCE.getString(R.string.strRegisterActivity211));
//                registerGetAuthCode.setEnabled(true);
//                if (!disposableSubscriber.isDisposed()) {
//                    disposableSubscriber.dispose();
//                    disposableSubscriber = null;
//                }
//
//            }
//        };
//
//        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
//                .take(60)
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(disposableSubscriber);
//    }

    @Override
    protected void onDestroy() {
        RxBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
